import { resolve } from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { visualizer } from 'rollup-plugin-visualizer'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// 经验：打包分析报告
// 安装rollup-plugin-visualizer -D，vite中引用+配置，package.json中配置npm run build:report
const buildReportplugin =
  process.env.npm_lifecycle_event === 'build:report'
    ? visualizer({ open: true, brotliSize: true, gzipSize: true, filename: 'dist/report.html' })
    : null

// https://vitejs.dev/config/
export default defineConfig({
  base: '/humanResource/',
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    }),
    buildReportplugin, // 打包分析报告
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [resolve(process.cwd(), 'src/icons/svg')],
      // 指定symbolId格式
      symbolId: 'icon-[dir]-[name]'
    })
  ],
  resolve: {
    // 实际路径转换 @ -> src
    alias: {
      '@': resolve('src') + '/'
      // '@':fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    open: false,
    port: 5858,

    // 设置 https 代理
    proxy: {
      '/api': {
        target: 'https://heimahr.itheima.net', // 指向地址
        secure: false,
        changeOrigin: true // 是否允许跨域
        // rewrite: (path: string) => path.replace(/^\/api/, '') // 重写路径
      }
    }
  }
})

// vite.config.ts
